unit EdCompra;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, cxCurrencyEdit, cxDBEdit, cxTextEdit, cxMaskEdit,
  cxCalendar, cxLabel, cxDBLabel, cxContainer, cxGroupBox, FMTBcd,
  DBClient, Provider, SqlExpr, cxNavigator, cxDBNavigator,
  cxLookAndFeelPainters, StdCtrls, cxButtons, ExtCtrls;

type
  TfrmEdCompra = class(TForm)
    cxGroupBox2: TcxGroupBox;
    lblNumero: TcxDBLabel;
    cxGroupBox4: TcxGroupBox;
    deData: TcxDBDateEdit;
    cxGroupBox11: TcxGroupBox;
    ceCliente: TcxDBCurrencyEdit;
    cxGroupBox1: TcxGroupBox;
    lkLocalCliente: TcxDBLookupComboBox;
    cxGroupBox3: TcxGroupBox;
    cxDBCurrencyEdit1: TcxDBCurrencyEdit;
    cxDBLookupComboBox1: TcxDBLookupComboBox;
    cxGroupBox26: TcxGroupBox;
    cdDespAcessItem: TcxDBCurrencyEdit;
    cxDBCurrencyEdit2: TcxDBCurrencyEdit;
    cxGroupBox6: TcxGroupBox;
    gdItens: TcxGrid;
    gdItensDBTableView1: TcxGridDBTableView;
    colItensCodigo: TcxGridDBColumn;
    colItensDesc: TcxGridDBColumn;
    colItensVlrUnit: TcxGridDBColumn;
    colItensQtd: TcxGridDBColumn;
    colItensVlrTotal: TcxGridDBColumn;
    gdItensLevel1: TcxGridLevel;
    sqlCompras: TSQLDataSet;
    dspCompras: TDataSetProvider;
    cdsCompras: TClientDataSet;
    dsCompras: TDataSource;
    dsItens: TDataSource;
    cdsItens: TClientDataSet;
    dspItens: TDataSetProvider;
    sqlItens: TSQLDataSet;
    sqlComprasSequencial: TIntegerField;
    sqlComprasNumero: TIntegerField;
    sqlComprasData: TSQLTimeStampField;
    sqlComprasOperacao: TIntegerField;
    sqlComprasPessoa: TIntegerField;
    sqlComprasValor: TFMTBCDField;
    cdsComprasSequencial: TIntegerField;
    cdsComprasNumero: TIntegerField;
    cdsComprasData: TSQLTimeStampField;
    cdsComprasOperacao: TIntegerField;
    cdsComprasPessoa: TIntegerField;
    cdsComprasValor: TFMTBCDField;
    sqlItensSeqCompra: TIntegerField;
    sqlItensSequencial: TIntegerField;
    sqlItensProduto: TIntegerField;
    sqlItensValorUnit: TFMTBCDField;
    sqlItensQtd: TFMTBCDField;
    sqlItensValorTotal: TFMTBCDField;
    cdsItensSeqCompra: TIntegerField;
    cdsItensSequencial: TIntegerField;
    cdsItensProduto: TIntegerField;
    cdsItensValorUnit: TFMTBCDField;
    cdsItensQtd: TFMTBCDField;
    cdsItensValorTotal: TFMTBCDField;
    sqlPessoas: TSQLDataSet;
    dspPessoas: TDataSetProvider;
    cdsPessoas: TClientDataSet;
    dsPessoas: TDataSource;
    sqlPessoasCodigo: TIntegerField;
    sqlPessoasNome: TStringField;
    cdsPessoasCodigo: TIntegerField;
    cdsPessoasNome: TStringField;
    dsOperacoes: TDataSource;
    cdsOperacoes: TClientDataSet;
    dspOperacoes: TDataSetProvider;
    sqlOperacoes: TSQLDataSet;
    sqlOperacoesCodigo: TIntegerField;
    sqlOperacoesDescricao: TStringField;
    cdsOperacoesCodigo: TIntegerField;
    cdsOperacoesDescricao: TStringField;
    sqlProdutos: TSQLDataSet;
    IntegerField1: TIntegerField;
    StringField1: TStringField;
    dspProdutos: TDataSetProvider;
    cdsProdutos: TClientDataSet;
    IntegerField2: TIntegerField;
    StringField2: TStringField;
    dsProdutos: TDataSource;
    sqlItensCodConfig: TIntegerField;
    cdsItensCodConfig: TIntegerField;
    Panel1: TPanel;
    btnConsulta: TcxButton;
    cxButton4: TcxButton;
    cxButton2: TcxButton;
    procedure FormShow(Sender: TObject);
    procedure QueryBasica(iSeq:Integer);
    procedure CarregaItens(iSeq:Integer);
    procedure dsComprasDataChange(Sender: TObject; Field: TField);
    procedure cdsComprasAfterPost(DataSet: TDataSet);
    procedure cdsComprasBeforePost(DataSet: TDataSet);
    procedure CarregaDataSets;
    procedure gdItensDBTableView1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure cdsItensAfterPost(DataSet: TDataSet);
    procedure cdsItensBeforePost(DataSet: TDataSet);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure gdItensDBTableView1DBColumn2PropertiesCloseUp(
      Sender: TObject);
    procedure cdsItensProdutoValidate(Sender: TField);
    procedure cdsItensValorUnitValidate(Sender: TField);
    procedure cdsItensQtdValidate(Sender: TField);
    procedure cdsItensAfterDelete(DataSet: TDataSet);
    procedure btnConsultaClick(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure cxButton4Click(Sender: TObject);
  private
    S:TStringList;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmEdCompra: TfrmEdCompra;

implementation
Uses Bib;
{$R *.dfm}

procedure TfrmEdCompra.FormShow(Sender: TObject);
begin
  S:=TStringList.Create;
  CarregaDataSets;
  QueryBasica(frmEdCompra.Tag);
end;

procedure TfrmEdCompra.QueryBasica(iSeq: Integer);
begin
  S.Clear;
  S.Add('Select Sequencial, Numero, Data, Operacao, Pessoa, Valor');
  S.Add('From Compras');
  S.Add('Where Sequencial = ' + IntToStr(iSeq));
  with cdsCompras do Begin
    Close;
    CommandText:=S.text;
    Open;
    if iSeq > 0 then begin
      Edit;
    end else begin
      Insert;
    end;
  end;
end;

procedure TfrmEdCompra.dsComprasDataChange(Sender: TObject; Field: TField);
begin
  if cdsCompras.RecordCount > 0 then begin
    CarregaItens(frmEdCompra.Tag);
  end;
end;

procedure TfrmEdCompra.CarregaItens(iSeq: Integer);
begin
  S.Clear;
  S.Add('Select SeqCompra, Sequencial, Produto, ValorUnit, Qtd, ValorTotal, CodConfig');
  S.Add('From ItensCompra');
  S.Add('Where SeqCompra = ' + IntToStr(iSeq));
  with cdsItens do Begin
    Close;
    CommandText:=S.text;
    Open;
  end;
end;

procedure TfrmEdCompra.cdsComprasAfterPost(DataSet: TDataSet);
begin
  cdsCompras.ApplyUpdates(0);
end;

procedure TfrmEdCompra.cdsComprasBeforePost(DataSet: TDataSet);
begin
  if cdsCompras.State = dsInsert then Begin
    cdsCompras.FieldByName('Sequencial').asINteger:=CarregaSeq('Compras','Sequencial');
  end;
end;

procedure TfrmEdCompra.CarregaDataSets;
begin
  cdsPessoas.Open;
  cdsOperacoes.Open;
  cdsProdutos.Open;
end;

procedure TfrmEdCompra.gdItensDBTableView1KeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if not (cdsCompras.State in [dsEdit,dsInsert]) then begin
    if (key=vk_INSERT) then begin
      colItensCodigo.FocusWithSelection;
      cdsItens.Insert;
    end else if (key=vk_DELETE) then begin
      cdsItens.Delete;
    end;
  end;
end;

procedure TfrmEdCompra.cdsItensAfterPost(DataSet: TDataSet);
begin
  cdsItens.ApplyUpdates(0);
  cdsCompras.Refresh;
end;

procedure TfrmEdCompra.cdsItensBeforePost(DataSet: TDataSet);
begin
  if cdsItens.State = dsInsert then begin
    cdsItens.FieldByName('SeqCompra').asINteger:=cdsCompras.FieldByName('Sequencial').asINteger;
    cdsItens.FieldByName('Sequencial').asINteger:=CarregaSeq('ItensCompra','Sequencial');
  end;
  cdsItens.FieldByName('CodConfig').asINteger:=BuscaConfigOper(cdsItens.FieldByName('Produto').asInteger,cdscompras.FieldByName('Operacao').asInteger);
end;

procedure TfrmEdCompra.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if activecontrol <> gdItens Then begin
    if Key = #13 then begin
      Key := #0;
      Perform(WM_NEXTDLGCTL, 0, 0);
    end;
  end;
end;

procedure TfrmEdCompra.gdItensDBTableView1DBColumn2PropertiesCloseUp(
  Sender: TObject);
begin
  ColItensQtd.FocusWithSelection;
  cdsItens.FieldByName('Qtd').asFloat:=1;
end;

procedure TfrmEdCompra.cdsItensProdutoValidate(Sender: TField);
begin
  if cdsItens.State = dsInsert then begin
    cdsItens.FieldByName('ValorUnit').asFloat:=BuscaPrecoProd(cdsItens.FieldByName('Produto').asInteger,cdsCompras.FieldByName('Pessoa').asINteger)
  end;
end;

procedure TfrmEdCompra.cdsItensValorUnitValidate(Sender: TField);
begin
  cdsItens.FieldByName('ValorTotal').asFloat:=cdsItens.FieldByName('ValorUnit').asFloat*cdsItens.FieldByName('Qtd').asFloat;
end;

procedure TfrmEdCompra.cdsItensQtdValidate(Sender: TField);
begin
  cdsItens.FieldByName('ValorTotal').asFloat:=cdsItens.FieldByName('ValorUnit').asFloat*cdsItens.FieldByName('Qtd').asFloat;
end;

procedure TfrmEdCompra.cdsItensAfterDelete(DataSet: TDataSet);
begin
  cdsItens.ApplyUpdates(0);
  cdsCompras.Refresh;  
end;

procedure TfrmEdCompra.btnConsultaClick(Sender: TObject);
begin
  if cdsCompras.State in [dsInsert,dsEdit] then begin
    cdsCompras.Post;
  end;
end;

procedure TfrmEdCompra.cxButton2Click(Sender: TObject);
begin
  if cdsCompras.State in [dsInsert,dsEdit] then begin
    cdsCompras.Cancel;
  end;
end;

procedure TfrmEdCompra.cxButton4Click(Sender: TObject);
begin
  Close;
end;

end.
